package com.zhuge_new.Dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.weigan.loopview.LoopView;
import com.weigan.loopview.OnItemSelectedListener;
import com.zhuge_new.Activity.CardExamineActivity;
import com.zhuge_new.R;

import java.util.ArrayList;

/**
 * Created by lenovo on 2018-09-05.
 */

public class PatmentDialog extends Dialog {
    private LoopView loopView;
    private Button btn_ok;
    private Button btn_cancle;
    private String dg_title;

    private OnSelectedListener listener;

    private ArrayList<String> stringArrayList = new ArrayList<>();
    private int position = 0;
    private TextView dialog_title;

    public PatmentDialog(Context context, ArrayList<String> arrayList) {
        super(context, R.style.Dialog_Msg);
        stringArrayList = arrayList;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.patmrnt_dialog);
        setCanceledOnTouchOutside(false);//设置显示dialog后,触屏屏幕不会使dialog消失
        loopView = (LoopView) findViewById(R.id.loopView);
        btn_ok = (Button) findViewById(R.id.btn_ok);
        btn_cancle = (Button) findViewById(R.id.btn_cancle);
        dialog_title = findViewById(R.id.dialog_title);
        loopView.setNotLoop();

        WindowManager m = getWindow().getWindowManager();
        Display d = m.getDefaultDisplay();
        WindowManager.LayoutParams p = getWindow().getAttributes();
        p.width = d.getWidth(); //设置dialog的宽度为当前手机屏幕的宽度
        getWindow().setAttributes(p);


        data();
        // 设置原始数据
        loopView.setItems(stringArrayList);
        // 滚动监听
        loopView.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                if (!TextUtils.isEmpty(stringArrayList.get(index))) {
                    position = index;//存储选择的位序
                }
            }
        });

        btn_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    if (!TextUtils.isEmpty(stringArrayList.get(position))) {
                        listener.getData(stringArrayList.get(position));
                    }
                }
                dismiss();
            }
        });

        btn_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
    }

    private void data() {
        if (dg_title==null){

        }else {
            dialog_title.setText(dg_title);
        }
    }


    public interface OnSelectedListener {
        void getData(String data);
    }

    public void setOnSelectedListener(OnSelectedListener listener) {
        this.listener = listener;
    }

    public void setTitle(String title) {
        dg_title=title;
    }
}
